<?php


class HttpClient
{
    const SaveCookie_Status = 0; //写入cookie文件
    const ReadCookie_Status = 1; //读取cookie至curl
    const SaveAndReadCookie_2_Status = 2; //写入文件1和文件2
    const SaveAndReadCookie_Status = 3; //都需要cookie文件
    const ReturnCurlInfo_Status = 5; //返回curl数组

    /**
     * curlString
     * @param UserModel $userModel 用户model
     * @param string $url  抓取的页面
     * @param string $referer 设置回掉的url
     * @param int $status 根据不同状态进行cookie文件切换
     * @param array $postData post请求
     * @param array $head //head头
     * @return bool|mixed|string
     */
    public static function request(UserModel $userModel, $url, $referer = "", $status = self::SaveAndReadCookie_Status, $postData = [], $head = [])
    {
        $cookFIle = $userModel->getCookieFile() . 'cookie.txt';
        $cookFIle1 = $userModel->getCookieFile() . 'cookie1.txt';
        $cookFIle2 = $userModel->getCookieFile() . 'cookie2.txt';
        $ch = curl_init();
        $user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        if ($referer) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }

        switch ($status) {
            case self::ReturnCurlInfo_Status:
            case self::ReadCookie_Status: //读取cooie文件
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookFIle);
                break;
            case self::SaveCookie_Status: //写入文件
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookFIle);
                break;
            case self::SaveAndReadCookie_Status:
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookFIle);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookFIle);
                break;
            case self::SaveAndReadCookie_2_Status:
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookFIle1);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookFIle2);
                break;
            default:
                break;
        }

        if (!empty($head)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
        }

        if (!empty($postData)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }

        $result = curl_exec($ch);
        if ($status == self::ReturnCurlInfo_Status) {
            $result = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        }
        curl_close($ch);
        return $result;
    }


    /**
     * 返回xhr的head头
     * @return array
     */
    public static function getXhrHead()
    {
        return ['application/x-www-form-urlencoded; charset=UTF-8'];
    }


    /**
     * 根据code进去判断
     * @param $code
     * @param int $status
     * @return array
     */
    public static function getCodeData($code, $status = self::SaveCookie_Status)
    {
        if ($status == self::SaveCookie_Status) {
            return array('code' => $code);
        }
        if ($status == self::SaveAndReadCookie_2_Status) {
            return $code;
        }
        return [];
    }
}